//


#import "NT_AddressViewController.h"
#import "NT_AddressAddViewController.h"
#import "NT_AddressTableViewCell.h"

@interface NT_AddressViewController () <UITableViewDataSource,UITableViewDelegate,NT_AddressTableDelegate>
/** Description*/
@property (nonatomic, strong) UITableView *bookTableView;
/** Description*/
@property (nonatomic, strong) NSMutableArray *dataSource;
/** */
@property (nonatomic, strong) NT_CoreDataBase *data;
/** */
@property (nonatomic, assign) BOOL  isshow;
@end

@implementation NT_AddressViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    _data = [[NT_CoreDataBase alloc]initWithCoreData:@"AddressModel" modelName:@"NoteData" success:^{} fail:^(NSError * _Nullable error) {}];
    [self getData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setnavBatTitle:@"Address"];
    //
    __weak typeof(self) weakSelf = self;
    [self nt_setupLeftAndRightwrurSetting:@"Add" block:^{
        NT_AddressAddViewController *vc = [[NT_AddressAddViewController alloc]init];
        [weakSelf.navigationController pushViewController:vc animated:YES];
    }];
    
    [self.view addSubview:self.bookTableView];
}


//#pragma mark ----------------- tableViewDelegate ----------------
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"cellIdentifier";
    NT_AddressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[NT_AddressTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.delegate = self;
    cell.model = _dataSource[indexPath.row];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return kRelativeHeight(10);
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}
//MARK:delegate
- (void)editActionCell:(NT_AddressTableViewCell *)cell {
    
    NT_AddressAddViewController *vc = [[NT_AddressAddViewController alloc]init];
    vc.model = cell.model;
    [self.navigationController pushViewController:vc animated:YES];
}
//shanchu
- (void)deleteActionCell:(NT_AddressTableViewCell *)cell {
    __weak typeof(self) weakSelf = self;
    [self.data ntDeleteEntity:nil key:[NSString stringWithFormat:@"id = '%@'",cell.model.id] success:^{
        [weakSelf getData];
    } fail:^(NSError * _Nullable error) {}];
}

//
- (UITableView *)bookTableView {
    if (!_bookTableView) {
        _bookTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, UI_NAV_BAR_HEIGHT, kMainScreenWidth, kMainScreenHeight) style:UITableViewStylePlain];
        _bookTableView.dataSource = self;
        _bookTableView.delegate   = self;
        _bookTableView.tableFooterView = [UIView new];
        _bookTableView.rowHeight  = kRelativeHeight(130);
    }
    return _bookTableView;
}
- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray new];
    }
    return _dataSource;
}
//MARK:读取数据
- (void)getData {
    
    [_data ntReadEntity:nil ascending:NO filterStr:nil success:^(NSArray * _Nonnull results) {
        if (!results) return ;
        self.dataSource = [results copy];
        [self.bookTableView reloadData];
        
        if (self.dataSource.count <= 0 && !self.isshow) {
            self.isshow = YES;
            NT_AddressAddViewController *vc = [[NT_AddressAddViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
        
    } fail:^(NSError * _Nullable error) {}];
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */
- (BOOL)nt_isShowLeft {
    return NO;
}

@end
